package org.jcs.simulation.neds.gui;

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;

public class HoldButton extends JButton {

        private int lengthDown;

        public int getLengthDown() {
            return lengthDown;
        }
	private class HeldDownMouseListener extends Thread implements MouseListener {

        private boolean mouseIsHeldDown;
        private String action;

        private HoldButton button;

        private long millis;

        public HeldDownMouseListener(HoldButton button, long millis) {
            this.button = button;
            this.millis = millis;
            start();
        }

        @Override
        public void mouseClicked(MouseEvent arg0) { }

        @Override
        public void mouseEntered(MouseEvent arg0) { }

        @Override
        public void mouseExited(MouseEvent arg0) { }
        
        public void setActionCommand(String command)
        {
        	action = command;
        }
        
        public void run()
        {
        	while(true)
        	{
	        	lengthDown = 1;
                        while (mouseIsHeldDown) {
                        lengthDown++;
	                button.fireActionPerformed(new ActionEvent(button, ActionEvent.ACTION_PERFORMED, action));
	                try {
	                    Thread.sleep(millis);
	                } catch (InterruptedException e) {
	                    e.printStackTrace();
	                    continue;
	                }
	        	}
	        	try {
                    Thread.sleep(millis);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    continue;
                }
        	}
        }
        
        @Override
        public void mousePressed(MouseEvent arg0) {
            mouseIsHeldDown = true;
//          This should be run in a sub thread?
          /*while (mouseIsHeldDown) {
              button.fireActionPerformed(new ActionEvent(button, ActionEvent.ACTION_PERFORMED, action));
              try {
                  Thread.sleep(millis);
              } catch (InterruptedException e) {
                  e.printStackTrace();
                  continue;
              }
          }*/
        }
        

        @Override
        public void mouseReleased(MouseEvent arg0) {
            mouseIsHeldDown = false;
        }

    }

    public HoldButton() {
        addHeldDownMouseListener();
    }

    public HoldButton(Icon icon) {
        super(icon);
        addHeldDownMouseListener();
    }

    public HoldButton(String text) {
        super(text);
        addHeldDownMouseListener();
    }

    public HoldButton(Action a) {
        super(a);
        addHeldDownMouseListener();
    }

    private void addHeldDownMouseListener() {
        addMouseListener(new HeldDownMouseListener(this, 300));
    }

}
